from django.utils.decorators import method_decorator
from django_redis import get_redis_connection
from rest_framework import status, viewsets
from rest_framework.response import Response
from apps.ten_auth.models import Enterprise, City, User
from rest_framework.views import APIView
from apps.ten_auth.models import Recruit
from rest_framework.response import Response
from utils.simmiddleware01 import login_required_my


# 推荐职位
class RecommendView(APIView):
	def get(self, request):
		rec = Recruit.objects.all()
		bb = []
		for i in rec:
			bb.append({
				"id": i.id,
				"jobname": i.jobname,
				"salary": i.salary,
				"condition": i.condition,
				"education": i.education,
				"type": i.type,
				"city": i.city,
				"createtime": i.createtime,
				"enterprise": {
					"id": i.enterprise.id,
					"name": i.enterprise.name,
					"labels": i.enterprise.labels,
					"logo": i.enterprise.logo,
					"summary": i.enterprise.summary,
					"recruits": []
				},
				"labels": i.labels
			})
		return Response({"errmsg": 0, "data": bb})


# 最新职位  /recruits/search/latest/

class LatestView(APIView):
	def get(self, request):
		rec = Recruit.objects.all()
		bb = []
		for i in rec:
			bb.append({
				"id": i.id,
				"jobname": i.jobname,
				"salary": i.salary,
				"condition": i.condition,
				"education": i.education,
				"type": i.type,
				"city": i.city,
				"createtime": i.createtime,
				"enterprise": {
					"id": i.enterprise.id,
					"name": i.enterprise.name,
					"labels": i.enterprise.labels,
					"logo": i.enterprise.logo,
					"summary": i.enterprise.summary,
					"recruits": []
				},
				"labels": i.labels
			})
		return Response({"errmsg": 0, "data": bb})


# 热门企业  /enterprise/search/hotlist/

class EnterpriseView(APIView):
	def get(self, request):
		list1 = []
		enterprise = Enterprise.objects.all()
		for i in enterprise:
			list1.append({
				"id": i.id,
				"name": i.name,
				"labels": i.labels,
				"logo": i.logo,
				"recruits": [j.id for j in i.recruits.all()],
				"summary": i.summary
			})
		return Response({"errmsg": 0, "data": list1})


# 热门城市 /city/hotlist/

class CityView(APIView):
	def get(self, request):
		city = City.objects.filter(ishot=1).all()
		list1 = []
		for i in city:
			list1.append({
				"id": i.id,
				"name": i.name,
				"ishot": i.ishot
			})
		return Response({"errmsg": 0, "data": list1})


# 搜索职位  recruits/search/city/keyword/
class SearchView(APIView):
	def post(self, request):
		keyword = request.data.get('keyword')
		cityname = request.data.get('cityname')
		rec = Recruit.objects.filter(city=cityname).all()
		list1 = []
		for i in rec:
			list1.append({
				"id": i.id,
				"jobname": i.jobname,
				"salary": i.salary,
				"condition": i.condition,
				"education": i.education,
				"type": i.type,
				"city": i.city,
				"createtime": i.createtime,
				"enterprise": {
					"id": i.enterprise.id,
					"name": i.enterprise.name,
					"labels": i.enterprise.labels,
					"logo": i.enterprise.logo,
					"summary": i.enterprise.summary,
					"recruits": []
				},
				"labels": i.labels
			})
		if not keyword:
			return Response({"errmsg": 0, "data": list1})
		list2 = []
		for i in list1:
			if keyword.lower() in i.get('jobname').lower():
				list2.append(i)
		return Response({"errmsg": 0, "data": list2})


# 职位详情     /recruits/<id>/
class DetailsView(APIView):
	def get(self, request, id):
		users = User.objects.all()

		rec = Recruit.objects.get(id=id)
		rec_dict = {
			"id": rec.id,
			"enterprise": {
				"id": rec.enterprise.id,
				"recruits": {
					"id": rec.id,
					"jobname": rec.jobname,
					"salary": rec.salary,
					"condition": rec.condition,
					"education": rec.education,
					"type": rec.type,
					"city": rec.city,
					"createtime": rec.createtime,
					"enterprise": {
						"id": rec.enterprise.id,
						"name": rec.enterprise.name,
						"labels": rec.enterprise.labels,
						"logo": rec.enterprise.logo,
						"recruits": [],
						"summary": rec.enterprise.summary,
					},
					"labels": rec.labels,
				},
				"name": rec.enterprise.name,
				"summary": rec.enterprise.summary,
				"content": rec.enterprise.content,
				"city": rec.enterprise.city,
				"address": rec.enterprise.address,
				"labels": rec.enterprise.labels,
				"coordinate": rec.enterprise.coordinate,
				"logo": rec.enterprise.logo,
				"url": rec.enterprise.url,
				"visits": rec.enterprise.visits,
				"users": []
			},
			"jobname": rec.jobname,
			"salary": rec.salary,
			"condition": rec.condition,
			"education": rec.education,
			"type": rec.type,
			"city": rec.city,
			"address": rec.address,
			"state": rec.state,
			"labels": rec.labels,
			"detailcontent": rec.detailcontent,
			"detailrequire": rec.detailrequire,
			"visits": rec.visits + 1,
			"createtime": rec.createtime,
			"users": [i.id for i in rec.users.all()]
		}
		return Response({"errmsg": 0, "data": rec_dict})


# 增加访问次数
class GetUsersView(APIView):
	def put(self, request, id):
		rec = Recruit.objects.get(id=id)
		rec.visits += 1
		rec.save()
		return Response({"errmsg": 0, "message": "更新成功", "success": "true"})


# 收藏职位   /recruits/{id}/collect/
class CollectView(APIView):
	@method_decorator(login_required_my, name='post')
	def post(self, request, id):
		rec = Recruit.objects.get(id=id)
		user_id = request.session.id
		user = User.objects.get(id=user_id)
		rec.users.add(user)
		rec.save()
		return Response({"errmsg": 0, "message": "收藏成功", "success": True})


# 取消收藏
class CancelcollectView(APIView):
	@method_decorator(login_required_my, name='post')
	def post(self, request, id):
		rec = Recruit.objects.get(id=id)
		user_id = request.session.id
		user = User.objects.get(id=user_id)
		rec.users.remove(user)
		rec.save()
		return Response({"errmsg": 0, "message": "取消收藏成功", "success": True})


# 企业详情 /enterprise/<int:id>/
class ParticularsView(APIView):
	def get(self, request, id):
		rec = Recruit.objects.get(id=id)
		ent = Enterprise.objects.get(id=id)
		list1 = []
		for i in ent.recruits.all():
			list1.append({
				"id": i.id,
				"jobname": i.jobname,
				"city": i.city,
				"salary": i.salary,
				"condition": i.condition,
				"education": i.education,
				"type": i.type,
				"labels": i.labels,
			})
		data = {
			"id": ent.id,
			"recruits": list1,
			"name": ent.name,
			"summary": ent.summary,
			"content": ent.content,
			"city": ent.city,
			"address": ent.address,
			"labels": ent.labels,
			"coordinate": ent.coordinate,
			"logo": ent.logo,
			"url": ent.url,
			"visits": ent.visits + 1,
			"users": [user.id for user in ent.users.all()],
		}
		return Response({"data": data, "errmsg": 0})


# 增加企业访问次数 /enterprise/<int:id>/visit/
class TutorialView(APIView):
	def put(self, request, id):
		ent = Enterprise.objects.get(id=id)
		ent.visits += 1
		ent.save()
		return Response({"errmsg": 0, "message": "更新成功", "success": "true"})


# 收藏企业   /recruits/{id}/collect/
class CollectView2(APIView):
	@method_decorator(login_required_my, name='post')
	def post(self, request, id):
		ent = Enterprise.objects.get(id=id)
		user_id = request.session.id
		user = User.objects.get(id=user_id)
		ent.users.add(user)
		ent.save()
		return Response({"errmsg": 0, "message": "收藏成功", "success": True})


# 取消收藏
class CancelcollectView2(APIView):
	@method_decorator(login_required_my, name='post')
	def post(self, request, id):
		ent = Enterprise.objects.get(id=id)
		user_id = request.session.id
		user = User.objects.get(id=user_id)
		ent.users.remove(user)
		ent.save()
		return Response({"errmsg": 0, "message": "取消收藏成功", "success": True})
